<?php
/*
Gets the html table to manage people.
*/
function get_people_manage_table($people,$controller)
{
	$CI =& get_instance();
	$table='<table class="tablesorter" id="sortable_table">';
	
	$headers = array('<input type="checkbox" id="select_all" />', 
	$CI->lang->line('common_last_name'),
	$CI->lang->line('common_first_name'),
	$CI->lang->line('common_email'),
	$CI->lang->line('common_phone_number'),
	'&nbsp');
	
	$table.='<thead><tr>';
	foreach($headers as $header)
	{
		$table.="<th>$header</th>";
	}
	$table.='</tr></thead><tbody>';
	$table.=get_people_manage_table_data_rows($people,$controller);
	$table.='</tbody></table>';
	return $table;
}

/*
Gets the html data rows for the people.
*/
function get_people_manage_table_data_rows($people,$controller)
{
	$CI =& get_instance();
	$table_data_rows='';
	
	foreach($people->result() as $person)
	{
		$table_data_rows.=get_person_data_row($person,$controller);
	}
	
	if($people->num_rows()==0)
	{
		$table_data_rows.="<tr><td colspan='6'><div class='warning_message' style='padding:7px;'>".$CI->lang->line('common_no_persons_to_display')."</div></tr></tr>";
	}
	
	return $table_data_rows;
}

function get_person_data_row($person,$controller)
{
	$CI =& get_instance();
	$controller_name=$CI->uri->segment(1);
	$width = $controller->get_form_width();
	//$width = ;

	$table_data_row='<tr>';
	$table_data_row.="<td width='5%'><input type='checkbox' id='person_$person->person_id' value='".$person->person_id."'/></td>";
	$table_data_row.='<td width="20%">'.ucfirst(strtolower(character_limiter($person->last_name,13))).'</td>';
	$table_data_row.='<td width="20%">'.ucfirst(strtolower(character_limiter($person->first_name,13))).'</td>';
	$table_data_row.='<td width="30%">'.mailto($person->email,character_limiter($person->email,22)).'</td>';
	$table_data_row.='<td width="20%">'.$person->phone_number.'</td>';		
	$table_data_row.='<td width="5%">'.anchor($controller_name."/view/$person->person_id/width:$width", $CI->lang->line('common_edit'),array('class'=>'thickbox','title'=>$CI->lang->line($controller_name.'_update'))).'</td>';		
	$table_data_row.='</tr>';
	
	return $table_data_row;
}



/*
Gets the html table to manage items.
*/
function get_items_manage_table($items,$controller)
{
	$CI =& get_instance();
	$table='<table class="tablesorter" id="sortable_table">';
	
	$headers = array('<input type="checkbox" id="select_all" />', 
	$CI->lang->line('items_name'),
	$CI->lang->line('items_brand'),
	$CI->lang->line('items_category'),
	$CI->lang->line('items_cost_price'),
	$CI->lang->line('items_unit_price'),
	$CI->lang->line('items_tax_percents'),
	$CI->lang->line('items_quantity'),
	'&nbsp');
	
	$table.='<thead><tr>';
	foreach($headers as $header)
	{
		$table.="<th>$header</th>";
	}
	$table.='</tr></thead><tbody>';
	$table.=get_items_manage_table_data_rows($items,$controller);
	$table.='</tbody></table>';
	return $table;
}

/*
Gets the html data rows for the items.
*/
function get_items_manage_table_data_rows($items,$controller)
{
	$CI =& get_instance();
	$table_data_rows='';
	
	foreach($items->result() as $item)
	{
		$table_data_rows.=get_item_data_row($item,$controller);
	}
	
	if($items->num_rows()==0)
	{
		$table_data_rows.="<tr><td colspan='8'><div class='warning_message' style='padding:7px;'>".$CI->lang->line('items_no_items_to_display')."</div></tr></tr>";
	}
	return $table_data_rows;
}

function get_item_data_row($item,$controller)
{
	$CI =& get_instance();
	$item_tax_info=$CI->Item_taxes->get_info($item->item_id);
	$tax_percents = '';
	foreach($item_tax_info as $tax_info)
	{
		$tax_percents.=$tax_info['percent']. '%, ';
	}
	$tax_percents=substr($tax_percents, 0, -2);
	$controller_name=$CI->uri->segment(1);
	$width = $controller->get_form_width();
	
	$reorder_level = $item->reorder_level;
	$quantity = $item->quantity;
	if($quantity <= 0)
	{
		$quantity_format = "<font color=red >$quantity (Out Of Stock)</font>";
	}
	else if($quantity == $reorder_level)
	{
		$quantity_format = "<font color=green >$quantity (Reorder)</font>";
	}
	else
	{
		$quantity_format = $quantity;
	}
	
	$table_data_row='<tr>';
	$table_data_row.="<td width='3%'><input type='checkbox' id='item_$item->item_id' value='".$item->item_id."'/></td>";
	$table_data_row.='<td width="15%">'.$item->name.'</td>';
	$table_data_row.='<td width="12%">'.$item->brand.'</td>';
	$table_data_row.='<td width="12%">'.$item->category.'</td>';
	$table_data_row.='<td width="12%">'.to_currency($item->cost_price,$controller->config->item('currency_inuse')).'</td>';
	$table_data_row.='<td width="12%">'.to_currency($item->unit_price,$controller->config->item('currency_inuse')).'</td>';
	$table_data_row.='<td width="12%">'.$tax_percents.'</td>';	
	$table_data_row.='<td width="16%">'.$quantity_format.'</td>';
	$table_data_row.='<td width="5%">'.anchor($controller_name."/view/$item->item_id/width:$width", $CI->lang->line('common_edit'),array('class'=>'thickbox','title'=>$CI->lang->line($controller_name.'_update'))).'</td>';		
	$table_data_row.='</tr>';
	
	return $table_data_row;
}

//////////////// Ordering //////////////////////////////////////////////////////////////

/*
Gets the html table to manage items.
*/
function order_items_manage_table($items,$controller)
{
	$CI =& get_instance();
	$table='<table class="tablesorter" id="sortable_table">';
	
	$headers = array( 
	$CI->lang->line('items_name'),
	$CI->lang->line('items_brand'),
	$CI->lang->line('items_category'),
	$CI->lang->line('items_cost_price').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('order_quantity'));
	
	$table.='<thead><tr>';
	foreach($headers as $header)
	{
		$table.="<th>$header</th>";
	}
	$table.='</tr></thead><tbody>';
	$table.=order_items_manage_table_data_rows($items,$controller);
	$table.='</tbody></table>';
	return $table;
}

/*
Gets the html data rows for the items.
*/
function order_items_manage_table_data_rows($items,$controller)
{
	$CI =& get_instance();
	$table_data_rows='';
	
	foreach($items->result() as $item)
	{
		$table_data_rows.=order_item_data_row($item,$controller);
	}
	
	if($items->num_rows()==0)
	{
		$table_data_rows.="<tr><td colspan='8'><div class='warning_message' style='padding:7px;'>".$CI->lang->line('items_no_items_to_display')."</div></tr></tr>";
	}
	return $table_data_rows;
}

function order_item_data_row($item,$controller)
{
	$CI =& get_instance();
	$item_tax_info=$CI->Item_taxes->get_info($item->item_id);
	$tax_percents = '';
	foreach($item_tax_info as $tax_info)
	{
		$tax_percents.=$tax_info['percent']. '%, ';
	}
	$tax_percents=substr($tax_percents, 0, -2);
	$controller_name=$CI->uri->segment(1);
	$width = $controller->get_form_width();
	
	$order_quantity = "<input type='text' name='itemqty[]' value='1'/>";
	$order_cost_price = "<input type='text' name='itemcost[]' value='".$item->cost_price."'/><input type='hidden' name='item[]' value='".$item->item_id."'/>";
	
	$table_data_row='<tr>';
	$table_data_row.='<td width="20%">'.$item->name.'</td>';
	$table_data_row.='<td width="20%">'.$item->brand.'</td>';
	$table_data_row.='<td width="20%">'.$item->category.'</td>';
	$table_data_row.='<td width="20%">'.$order_cost_price.'</td>';	
	$table_data_row.='<td width="40%">'.$order_quantity.'</td>';
	$table_data_row.='</tr>';
	
	return $table_data_row;
}

///////////////////////////////// Printing Order /////////////////////////////////////////

/*
Gets the html table to manage items.
*/
function print_order_items_manage_table($items,$controller)
{
	$CI =& get_instance();
	$table='<table class="tablesorter" id="sortable_table">';
	
	$headers = array( 
	$CI->lang->line('items_name'),
	$CI->lang->line('items_brand'),
	$CI->lang->line('items_category'),
	$CI->lang->line('items_cost_price').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('order_quantity'),
	$CI->lang->line('sub_total').' '.$controller->config->item('currency_inuse')
	);
	
	$table.='<thead><tr>';
	foreach($headers as $header)
	{
		$table.="<th>$header</th>";
	}
	$table.='</tr></thead><tbody>';
	$table.=print_order_items_manage_table_data_rows($items,$controller);
	$table.='</tbody></table>';
	return $table;
}

/*
Gets the html data rows for the items.
*/
function print_order_items_manage_table_data_rows($items,$controller)
{
	$CI =& get_instance();
	$table_data_rows='';
	
	foreach($items->result() as $item)
	{
		$table_data_rows.=print_order_item_data_row($item,$controller);
	}
	
	//Get the total cost of order
	$output = array();
	foreach($items->result() as $row)
	{
		$output[]=$row->item_total;
	}
	$sum = to_currency(array_sum($output),$controller->config->item('currency_inuse'));
	
	$table_data_rows.='<tr>';
	$table_data_rows.='<td width="15%">&nbsp;</td>';
	$table_data_rows.='<td width="15%"></td>';
	$table_data_rows.='<td width="15%"></td>';
	$table_data_rows.='<td width="15%"></td>';
	$table_data_rows.='<td width="15%"></td>';
	$table_data_rows.='<td width="40%"></td>';
	$table_data_rows.='</tr>';
	
	$table_data_rows.='<tr>';
	$table_data_rows.='<td width="15%"></td>';
	$table_data_rows.='<td width="15%"></td>';
	$table_data_rows.='<td width="15%"></td>';
	$table_data_rows.='<td width="15%"></td>';
	$table_data_rows.='<td width="15%"></td>';
	$table_data_rows.='<td width="40%"><strong>'.$CI->lang->line('total').' '.$sum.'</strong></td>';
	$table_data_rows.='</tr>';
				
	
	if($items->num_rows()==0)
	{
		$table_data_rows.="<tr><td colspan='8'><div class='warning_message' style='padding:7px;'>".$CI->lang->line('items_no_items_to_display')."</div></tr></tr>";
	}
	return $table_data_rows;
}

function print_order_item_data_row($item,$controller)
{
	
	$table_data_row='<tr>';
	$table_data_row.='<td width="15%">'.$item->name.'</td>';
	$table_data_row.='<td width="15%">'.$item->brand.'</td>';
	$table_data_row.='<td width="15%">'.$item->category.'</td>';
	$table_data_row.='<td width="15%">'.$item->order_item_cost.'</td>';	
	$table_data_row.='<td width="15%">'.$item->order_quantity.'</td>'; 	
	$table_data_row.='<td width="40%">'.number_format($item->item_total, 2, '.', '').'</td>';
	$table_data_row.='</tr>';
	
	return $table_data_row;
}


//////////////////// Managing of orders///////////////////////////

/*
Gets the html table to manage items.
*/
function manage_order_table($items,$controller)
{
	$CI =& get_instance();
	$table='<table class="tablesorter" id="sortable_table">';
	
	$headers = array('<input type="checkbox" id="select_all" />', 
	$CI->lang->line('date'),
	$CI->lang->line('suppliers_supplier'),
	$CI->lang->line('order_by'),
	$CI->lang->line('total').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('order_payment').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('order_balance').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('common_modify')
	);
	
	$table.='<thead><tr>';
	foreach($headers as $header)
	{
		$table.="<th>$header</th>";
	}
	$table.='</tr></thead><tbody>';
	$table.=manage_order_table_data_rows($items,$controller);
	$table.='</tbody></table>';
	return $table;
}

/*
Gets the html data rows for the items.
*/
function manage_order_table_data_rows($items,$controller)
{
	$CI =& get_instance();
	$table_data_rows='';
	
	foreach($items as $sort)
	{
		$table_data_rows.=manage_order_item_data_row($sort,$controller);
	}				
	
	if(empty($items))
	{
		$table_data_rows.="<tr><td colspan='8'><div class='warning_message' style='padding:7px;'>".$CI->lang->line('items_no_items_to_display')."</div></tr></tr>";
	}
	return $table_data_rows;
}

function manage_order_item_data_row($item,$controller)
{
	$CI =& get_instance();
	$controller_name=$CI->uri->segment(1);
	$item_id = $item[0];
	
	$width = 900;
	$width2 = $controller->get_form_width();
	
	if($item[5] >= $item[4])
	{
		$pay_lik = $CI->lang->line('order_pay');
	}
	else
	{
		$pay_lik = anchor($controller_name."/pay_form/$item_id/width:$width2", $CI->lang->line('order_pay'),array('class'=>'thickbox','title'=>$CI->lang->line('order_payc')));
	}
	
	$table_data_row='<tr>';
	$table_data_row.="<td width='3%'><input type='checkbox' id='item_$item_id' value='".$item_id."'/></td>";
	$table_data_row.='<td width="10%">'.$item[1].'</td>';
	$table_data_row.='<td width="10%">'.$item[2].'</td>';
	$table_data_row.='<td width="10%">'.$item[3].'</td>';
	$table_data_row.='<td width="10%">'.$item[4].'</td>';
	$table_data_row.='<td width="10%">'.$item[5].'</td>';
	$table_data_row.='<td width="10%">'.$item[6].'</td>';
	$table_data_row.='<td width="20%">'.$pay_lik.' &nbsp;&nbsp;'.anchor($controller_name."/edit_order/$item_id/width:$width", $CI->lang->line('common_edit'),array('class'=>'thickbox','title'=>$CI->lang->line('edit_order'))).' &nbsp;&nbsp;'.anchor($controller_name."/print_order/$item_id/", $CI->lang->line('common_print'),array('class'=>'','title'=>$CI->lang->line('print_order_transaction'),'target'=>'blank')).'</td>';		
	$table_data_row.='</tr>';
	
	return $table_data_row;
}


//////////////// Edit an Order //////////////////////////////////////////////////////////////

/*
Gets the html table to manage items.
*/
function edit_order_items_manage_table($items,$controller)
{
	$CI =& get_instance();
	$table='<table class="tablesorter" id="sortable_table">';
	
	$headers = array( 
	$CI->lang->line('items_name'),
	$CI->lang->line('items_brand'),
	$CI->lang->line('items_category'),
	$CI->lang->line('items_cost_price').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('order_quantity'));
	
	$table.='<thead><tr>';
	foreach($headers as $header)
	{
		$table.="<th>$header</th>";
	}
	$table.='</tr></thead><tbody>';
	$table.=edit_order_items_manage_table_data_rows($items,$controller);
	$table.='</tbody></table>';
	return $table;
}

/*
Gets the html data rows for the items.
*/
function edit_order_items_manage_table_data_rows($items,$controller)
{
	$CI =& get_instance();
	$table_data_rows='';
	
	foreach($items->result() as $item)
	{
		$table_data_rows.=edit_order_item_data_row($item,$controller);
	}
	
	if($items->num_rows()==0)
	{
		$table_data_rows.="<tr><td colspan='8'><div class='warning_message' style='padding:7px;'>".$CI->lang->line('items_no_items_to_display')."</div></tr></tr>";
	}
	return $table_data_rows;
}

function edit_order_item_data_row($item,$controller)
{
	$CI =& get_instance();
	$controller_name=$CI->uri->segment(1);
	$width = $controller->get_form_width();
	
	$order_quantity = "<input type='text' name='itemqty[]' value='".$item->order_quantity."'/>"; 
	$order_cost_price = "<input type='text' name='itemcost[]' value='".$item->order_item_cost."'/><input type='hidden' name='item[]' value='".$item->order_item_number."'/><input type='hidden' name='item_update_id[]' value='".$item->order_item_id."'/>";
	
	$table_data_row='<tr>';
	$table_data_row.='<td width="20%">'.$item->name.'</td>';
	$table_data_row.='<td width="20%">'.$item->brand.'</td>';
	$table_data_row.='<td width="20%">'.$item->category.'</td>';
	$table_data_row.='<td width="20%">'.$order_cost_price.'</td>';	
	$table_data_row.='<td width="40%">'.$order_quantity.'</td>';
	$table_data_row.='</tr>';
	
	return $table_data_row;
}


//////////////////////  SUPPLIERS MANAGE /////////////////////////
/*
Gets the html table to manage suppliers.
*/
function suppliers_people_manage_table($people,$controller)
{
	$CI =& get_instance();
	$table='<table class="tablesorter" id="sortable_table">';
	
	$headers = array('<input type="checkbox" id="select_all" />', 
	$CI->lang->line('suppliers_name'),
	$CI->lang->line('suppliers_status'),
	$CI->lang->line('common_email'),
	$CI->lang->line('common_phone_number'),
	$CI->lang->line('common_modify')
	);
	
	$table.='<thead><tr>';
	foreach($headers as $header)
	{
		$table.="<th>$header</th>";
	}
	$table.='</tr></thead><tbody>';
	$table.=suppliers_people_manage_table_data_rows($people,$controller);
	$table.='</tbody></table>';
	return $table;
}

/*
Gets the html data rows for the people.
*/
function suppliers_people_manage_table_data_rows($people,$controller)
{
	$CI =& get_instance();
	$table_data_rows='';
	
	foreach($people->result() as $person)
	{
		$table_data_rows.=suppliers_person_data_row($person,$controller);
	}
	
	if($people->num_rows()==0)
	{
		$table_data_rows.="<tr><td colspan='6'><div class='warning_message' style='padding:7px;'>".$CI->lang->line('common_no_persons_to_display')."</div></tr></tr>";
	}
	
	return $table_data_rows;
}

function suppliers_person_data_row($person,$controller)
{
	$CI =& get_instance();
	$controller_name=$CI->uri->segment(1);
	$width = $controller->get_form_width();
	//$width = ;

	$table_data_row='<tr>';
	$table_data_row.="<td width='5%'><input type='checkbox' id='person_$person->person_id' value='".$person->person_id."'/></td>";
	$table_data_row.='<td width="20%">'.ucfirst(strtolower(character_limiter($person->first_name,13))).'</td>';
	$table_data_row.='<td width="15%">'.ucfirst(strtolower(character_limiter($person->last_name,13))).'</td>';
	$table_data_row.='<td width="20%">'.mailto($person->email,character_limiter($person->email,22)).'</td>';
	$table_data_row.='<td width="15%">'.$person->phone_number.'</td>';		
	$table_data_row.='<td width="30%">'.anchor($controller_name."/view/$person->person_id/width:$width", $CI->lang->line('common_edit'),array('class'=>'thickbox','title'=>$CI->lang->line($controller_name.'_update'))).' &nbsp;&nbsp;'.anchor($controller_name."/open_account/$person->person_id/", $CI->lang->line('suppliers_open_account'),array('class'=>'','title'=>$CI->lang->line('suppliers_open'),'target'=>'blank')).'</td>';		
	$table_data_row.='</tr>';
	
	return $table_data_row;
}

/////////////////// Suppliers Acount //////////////////////
/*
Gets the html table to manage items.
*/
function supplier_acct_order_table($items,$controller)
{
	$CI =& get_instance();
	$table='<table class="tablesorter" id="sortable_table">';
	
	$headers = array('<input type="checkbox" id="select_all" />', 
	$CI->lang->line('date'),
	$CI->lang->line('order_by'),
	$CI->lang->line('total').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('order_payment').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('order_balance').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('common_modify')
	);
	
	$table.='<thead><tr>';
	foreach($headers as $header)
	{
		$table.="<th>$header</th>";
	}
	$table.='</tr></thead><tbody>';
	$table.=supplier_acct_order_table_data_rows($items,$controller);
	$table.='</tbody></table>';
	return $table;
}

/*
Gets the html data rows for the items.
*/
function supplier_acct_order_table_data_rows($items,$controller)
{
	$CI =& get_instance();
	$table_data_rows='';
	
	foreach($items as $sort)
	{
		$table_data_rows.=supplier_acct_order_item_data_row($sort,$controller);
	}

	//Get the item_total_cost 
	$item_total_cost = array();
	foreach($items as $row)
	{
		$item_total_cost[]=$row[3];//item_total
	}
	$sum_item_total_cost = to_currency(array_sum($item_total_cost),$controller->config->item('currency_inuse'));
	
	//Get the payment_total
	$payment_total = array();
	foreach($items as $row)
	{
		$payment_total[]=$row[4];//payment_total
	}
	$sum_payment_total = to_currency(array_sum($payment_total),$controller->config->item('currency_inuse'));
	
	//Get the balance_total
	$balance_total = array();
	foreach($items as $row)
	{
		$balance_total[]=$row[5];//payment_total
	}
	$sum_balance_total = to_currency(array_sum($balance_total),$controller->config->item('currency_inuse'));
	
	$table_data_rows.='<tr>';
	$table_data_rows.='<td width="3%">&nbsp;</td>';
	$table_data_rows.='<td width="10%"></td>';
	$table_data_rows.='<td width="10%"></td>';
	$table_data_rows.='<td width="10%"></td>';
	$table_data_rows.='<td width="10%"></td>';
	$table_data_rows.='<td width="10%"></td>';
	$table_data_rows.='<td width="20%"></td>';
	$table_data_rows.='</tr>';
	
	$table_data_rows.='<tr>';
	$table_data_rows.='<td width="3%"></td>';
	$table_data_rows.='<td width="10%"></td>';
	$table_data_rows.='<td width="10%"></td>';
	$table_data_rows.='<td width="10%"><strong>'.$CI->lang->line('total').' '.$sum_item_total_cost.'</strong></td>';
	$table_data_rows.='<td width="10%"><strong>'.$CI->lang->line('total').' '.$sum_payment_total.'</strong></td>';
	$table_data_rows.='<td width="10%"><strong>'.$CI->lang->line('total').' '.$sum_balance_total.'</strong></td>';
	$table_data_rows.='<td width="20%"></td>';
	$table_data_rows.='</tr>';
	
	if(empty($items))
	{
		$table_data_rows.="<tr><td colspan='8'><div class='warning_message' style='padding:7px;'>".$CI->lang->line('items_no_items_to_display')."</div></tr></tr>";
	}
	return $table_data_rows;
}

function supplier_acct_order_item_data_row($item,$controller)
{
	$CI =& get_instance();
	$controller_name=$CI->uri->segment(1);
	$item_id = $item[0];
	
	$width = 600;
	$width2 = 800;
	
	$pay_lik = anchor($controller_name."/order_payment_made/$item_id/width:$width", $CI->lang->line('suppliers_payment'),array('class'=>'thickbox','title'=>$CI->lang->line('order_payment')));
	
	$table_data_row='<tr>';
	$table_data_row.="<td width='3%'><input type='checkbox' id='item_$item_id' value='".$item_id."'/></td>";
	$table_data_row.='<td width="10%">'.$item[1].'</td>';
	//$table_data_row.='<td width="10%">'.$item[2].'</td>';
	$table_data_row.='<td width="10%">'.$item[2].'</td>';
	$table_data_row.='<td width="10%">'.$item[3].'</td>';
	$table_data_row.='<td width="10%">'.$item[4].'</td>';
	$table_data_row.='<td width="10%">'.$item[5].'</td>';
	$table_data_row.='<td width="20%">'.$pay_lik.' &nbsp;&nbsp;'.anchor($controller_name."/view_order_purchases/$item_id/width:$width2", $CI->lang->line('suppliers_purchases'),array('class'=>'thickbox','title'=>$CI->lang->line('common_purchases'))).' &nbsp;&nbsp;'.anchor($controller_name."/print_order/$item_id/", $CI->lang->line('common_print'),array('class'=>'','title'=>$CI->lang->line('print_order_transaction'),'target'=>'blank')).'</td>';		
	$table_data_row.='</tr>';
	
	return $table_data_row;
}



//////////////////  Listing all Payment for an order ///////////////////

function order_payment_table($people,$controller)
{
	$CI =& get_instance();
	$table='<table class="tablesorter" id="sortable_table">';
	
	$headers = array( 
	$CI->lang->line('date'),
	$CI->lang->line('order_invoice'),
	$CI->lang->line('order_payment').' '.$controller->config->item('currency_inuse')
	);
	
	$table.='<thead><tr>';
	foreach($headers as $header)
	{
		$table.="<th>$header</th>";
	}
	$table.='</tr></thead><tbody>';
	$table.=order_payment_table_data_rows($people,$controller);
	$table.='</tbody></table>';
	return $table;
}


function order_payment_table_data_rows($people,$controller)
{
	$CI =& get_instance();
	$table_data_rows='';
	
	foreach($people->result() as $person)
	{
		$table_data_rows.=order_payment_data_row($person,$controller);
	}
	
	if($people->num_rows()==0)
	{
		$table_data_rows.="<tr><td colspan='6'><div class='warning_message' style='padding:7px;'>".$CI->lang->line('common_no_persons_to_display')."</div></tr></tr>";
	}
	
	return $table_data_rows;
}

function order_payment_data_row($person,$controller)
{
	$CI =& get_instance();
	$controller_name=$CI->uri->segment(1);
	//$width = $controller->get_form_width();
	//$width = ;

	$table_data_row='<tr>';
	//$table_data_row.="<td width='5%'><input type='checkbox' id='person_$person->person_id' value='".$person->person_id."'/></td>";
	$table_data_row.='<td width="20%">'.$person->pay_date.'</td>';
	$table_data_row.='<td width="15%">'.$person->pay_invoice_no.'</td>';
	$table_data_row.='<td width="20%">'.$person->pay_amount.'</td>';
	//$table_data_row.='<td width="15%">'.character_limiter($person->phone_number,13).'</td>';		
	//$table_data_row.='<td width="30%">'.anchor($controller_name."/view/$person->person_id/width:$width", $CI->lang->line('common_edit'),array('class'=>'thickbox','title'=>$CI->lang->line($controller_name.'_update'))).' &nbsp;&nbsp;'.anchor($controller_name."/open_account/$person->person_id/", $CI->lang->line('suppliers_open_account'),array('class'=>'','title'=>$CI->lang->line($controller_name.'_update'),'target'=>'blank')).'</td>';		
	$table_data_row.='</tr>';
	
	return $table_data_row;
}


////////////// Statement of Account /////////////////////

function statement_acct_order_table($items,$controller)
{
	$CI =& get_instance();
	$table='<table class="tablesorter" id="sortable_table">';
	
	$headers = array( 
	$CI->lang->line('date'),
	$CI->lang->line('order_by'),
	$CI->lang->line('total').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('order_payment').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('order_balance').' '.$controller->config->item('currency_inuse'),
	);
	
	$table.='<thead><tr>';
	foreach($headers as $header)
	{
		$table.="<th>$header</th>";
	}
	$table.='</tr></thead><tbody>';
	$table.=statement_acct_order_table_data_rows($items,$controller);
	$table.='</tbody></table>';
	return $table;
}

/*
Gets the html data rows for the items.
*/
function statement_acct_order_table_data_rows($items,$controller)
{
	$CI =& get_instance();
	$table_data_rows='';
	
	foreach($items as $sort)
	{
		$table_data_rows.=statement_acct_order_item_data_row($sort,$controller);
	}	
	
	//Get the item_total_cost 
	$item_total_cost = array();
	foreach($items as $row)
	{
		$item_total_cost[]=$row[3];//item_total
	}
	$sum_item_total_cost = to_currency(array_sum($item_total_cost),$controller->config->item('currency_inuse'));
	
	//Get the payment_total
	$payment_total = array();
	foreach($items as $row)
	{
		$payment_total[]=$row[4];//payment_total
	}
	$sum_payment_total = to_currency(array_sum($payment_total),$controller->config->item('currency_inuse'));
	
	//Get the balance_total
	$balance_total = array();
	foreach($items as $row)
	{
		$balance_total[]=$row[5];//payment_total
	}
	$sum_balance_total = to_currency(array_sum($balance_total),$controller->config->item('currency_inuse'));
	
	$table_data_rows.='<tr>';
	$table_data_rows.='<td width="15%">&nbsp;</td>';
	$table_data_rows.='<td width="15%"></td>';
	$table_data_rows.='<td width="15%"></td>';
	$table_data_rows.='<td width="15%"></td>';
	$table_data_rows.='<td width="15%"></td>';
	$table_data_rows.='</tr>';
	
	$table_data_rows.='<tr>';
	$table_data_rows.='<td width="15%"></td>';
	$table_data_rows.='<td width="15%"></td>';
	$table_data_rows.='<td width="15%"><strong>'.$CI->lang->line('total').' '.$sum_item_total_cost.'</strong></td>';
	$table_data_rows.='<td width="15%"><strong>'.$CI->lang->line('total').' '.$sum_payment_total.'</strong></td>';
	$table_data_rows.='<td width="15%"><strong>'.$CI->lang->line('total').' '.$sum_balance_total.'</strong></td>';
	$table_data_rows.='</tr>';
	
	if(empty($items))
	{
		$table_data_rows.="<tr><td colspan='8'><div class='warning_message' style='padding:7px;'>".$CI->lang->line('items_no_items_to_display')."</div></tr></tr>";
	}
	return $table_data_rows;
}

function statement_acct_order_item_data_row($item,$controller)
{
	$CI =& get_instance();
	$controller_name=$CI->uri->segment(1);
	$item_id = $item[0];
	
	$table_data_row='<tr>';
	$table_data_row.='<td width="10%">'.$item[1].'</td>';
	$table_data_row.='<td width="10%">'.$item[2].'</td>';
	$table_data_row.='<td width="10%">'.$item[3].'</td>';
	$table_data_row.='<td width="10%">'.$item[4].'</td>';
	$table_data_row.='<td width="10%">'.$item[5].'</td>';	
	$table_data_row.='</tr>';
	
	return $table_data_row;
}

//////////////////////  STAFF  MANAGE /////////////////////////
require_once ("staff_table_helper.php");
//////////////////////  TAX MANAGE /////////////////////////
require_once ("tax_table_helper.php");
//////////////////////  SSF MANAGE /////////////////////////
require_once ("ssf_table_helper.php");
//////////////////////  REPORT HELPER /////////////////////////
require_once ("report_table_helper.php");
//////////////////////  BANK HELPER /////////////////////////
require_once ("banks_table_helper.php");


?>